package com.rm.wechatapp.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rm.wechatapp.entity.OnQuestion;
import com.rm.wechatapp.entity.VO.QuestionVO;
import com.rm.wechatapp.mapper.OnQuestionMapper;
import com.rm.wechatapp.service.IOnQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 题目Service业务层处理
 * 
 * @author sh
 * @date 2025-03-25
 */
@Service
public class OnQuestionServiceImpl extends ServiceImpl<OnQuestionMapper, OnQuestion> implements IOnQuestionService
{
    @Autowired
    private OnQuestionMapper onQuestionMapper;

    /**
     * 查询题目
     * 
     * @param id 题目主键
     * @return 题目
     */
    @Override
    public OnQuestion selectById(Long id)
    {
        return onQuestionMapper.selectById(id);
    }

    /**
     * 查询题目列表
     * 
     * @param onQuestion 题目
     * @return 题目
     */
    @Override
    public List<OnQuestion> selectList(OnQuestion onQuestion)
    {
        return onQuestionMapper.selectList(onQuestion);
    }

    /**
     * 新增题目
     * 
     * @param onQuestion 题目
     * @return 结果
     */
    @Override
    public int insert(OnQuestion onQuestion)
    {
        return onQuestionMapper.insert(onQuestion);
    }

    /**
     * 修改题目
     * 
     * @param onQuestion 题目
     * @return 结果
     */
    @Override
    public int update(OnQuestion onQuestion)
    {
        return onQuestionMapper.update(onQuestion);
    }

    /**
     * 批量删除题目
     * 
     * @param ids 需要删除的题目主键
     * @return 结果
     */
    @Override
    public int deleteByIds(String ids)
    {
        return onQuestionMapper.deleteByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除题目信息
     * 
     * @param id 题目主键
     * @return 结果
     */
    @Override
    public int deleteById(Long id)
    {
        return onQuestionMapper.deleteById(id);
    }

    /**
     * 新增题目+记录日志
     *
     * @param onQuestion 题目
     * @return 结果
     */
    public int insertAndLog(OnQuestion onQuestion){
        int result = onQuestionMapper.insert(onQuestion);
//        ShTkQLogs shTkQLogs = new ShTkQLogs();
//        shTkQLogs.setLogType("insert");
//        shTkQLogs.setPostQuestions();
//        shTkQLogs.setQuestionId(.getId());
//        shTkQLogsMapper.insertShTkQLogs(shTkQLogs);
        return result;
    }

    /**
     * 修改题目+记录日志
     *
     * @param onQuestion 题目
     * @return 结果
     */
    public int updateAndLog(OnQuestion onQuestion){
//        ShTkQLogs shTkQLogs = new ShTkQLogs();
//        shTkQLogs.setLogType("update");
//        shTkQLogs.setPreQuestions(Mapper.selectById(.getId()));
//        shTkQLogs.setPostQuestions();
//        shTkQLogs.setQuestionId(.getId());
//        shTkQLogsMapper.insertShTkQLogs(shTkQLogs);
        return onQuestionMapper.update(onQuestion);
    }

    /**
     * 批量删除题目+记录日志
     *
     * @param ids 需要删除的题目主键集合
     * @return 结果
     */
    public int deleteByIdsAndLog(String ids){
        String[] idArray = Convert.toStrArray(ids);
//        for(String id : idArray){
//            ShTkQLogs shTkQLogs = new ShTkQLogs();
//            shTkQLogs.setLogType("delete");
//            shTkQLogs.setPreQuestions(Mapper.selectById(Long.valueOf(id)));
//            shTkQLogs.setQuestionId(Long.valueOf(id));
//            shTkQLogsMapper.insertShTkQLogs(shTkQLogs);
//        }
        return onQuestionMapper.deleteByIds(idArray);
    }


    /**
     * 删除题目信息+记录日志
     *
     * @param id 题目主键
     * @return 结果
     */
    public int deleteByIdAndLog(Long id){
//        ShTkQLogs shTkQLogs = new ShTkQLogs();
//        shTkQLogs.setLogType("delete");
//        shTkQLogs.setPreQuestions(Mapper.selectById(Long.valueOf(id)));
//        shTkQLogs.setQuestionId(Long.valueOf(id));
//        shTkQLogsMapper.insertShTkQLogs(shTkQLogs);
        return onQuestionMapper.deleteById(Long.valueOf(id));
    }

    /**
     * 获取微信小程序需要的题目列表
     *
     * @param ownerId
     * @return
     */
    @Override
    public List<QuestionVO> selectByOwnerIdToVO(Long ownerId){
        List<OnQuestion> pre_list = selectByOwnerId(ownerId);
        List<QuestionVO> post_list = new ArrayList<QuestionVO>();
        int i = 1;
        for(OnQuestion onQuestion : pre_list) {
            QuestionVO vo = new QuestionVO();
            vo.setId(i);
            vo.setOwner_id(onQuestion.getOwnerId().toString());
            vo.setTitle(onQuestion.getTitle());
            vo.setOptions(JSONUtil.parseArray(onQuestion.getOptions()).toList(String.class));
            vo.setAnswer(JSONUtil.parseArray(onQuestion.getAnswer()).toList(String.class));
            vo.setExplain_skill(onQuestion.getExplainSkill());
            vo.setQuestion_explain(onQuestion.getQuestionExplain());
            vo.setImage(onQuestion.getImage());
            vo.setReal_id(onQuestion.getId());
            i++;
            post_list.add(vo);
        }
        return post_list;
    }

    /**
     * 根据所属题库ID查询题目
     * @param ownerId 所属题库ID
     * @return 题库信息集合
     */
    @Override
    public List<OnQuestion> selectByOwnerId(Long ownerId){
        return onQuestionMapper.selectByOwnerId(ownerId);
    }

    /**
     * 获取模拟考试100道题目
     *
     * @return 模拟考试题目
     */
    public List<QuestionVO> selectMockExam(){
        List<OnQuestion> pre_list = list();
        Collections.shuffle(pre_list);
        pre_list = pre_list.subList(0,100);
        List<QuestionVO> post_list = new ArrayList<QuestionVO>();
        int i = 1;
        for(OnQuestion onQuestion : pre_list) {
            QuestionVO vo = new QuestionVO();
            vo.setId(i);
            vo.setOwner_id(onQuestion.getOwnerId().toString());
            vo.setTitle(onQuestion.getTitle());
            vo.setOptions(JSONUtil.parseArray(onQuestion.getOptions()).toList(String.class));
            vo.setAnswer(JSONUtil.parseArray(onQuestion.getAnswer()).toList(String.class));
            vo.setExplain_skill(onQuestion.getExplainSkill());
            vo.setQuestion_explain(onQuestion.getQuestionExplain());
            vo.setImage(onQuestion.getImage());
            i++;
            post_list.add(vo);
        }
        return post_list;
    }

}
